Skip to content

Fixed an issue where EXPLAIN and EXPLAIN ANALYZE not working if blank line is there in the SQL query.#9670

Open
akshay-joshi wants to merge 1 commit intopgadmin-org:masterfrom
akshay-joshi:master
Open

Fixed an issue where EXPLAIN and EXPLAIN ANALYZE not working if blank line is there in the SQL query.#9670
akshay-joshi wants to merge 1 commit intopgadmin-org:masterfrom
akshay-joshi:master

Conversation

@akshay-joshi
Copy link
Contributor

@akshay-joshi akshay-joshi commented Feb 24, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced SQL query detection to accurately identify complete queries even with blank lines between clauses and comment blocks.
  • Tests

    • Added test coverage for query detection with various formatting scenarios, including blank lines and multi-line SQL statements.

@coderabbitai
Copy link

coderabbitai bot commented Feb 24, 2026

Walkthrough

The changes refactor query detection logic in CustomEditorView.js to properly handle blank lines and comments between SQL clauses. The getQueryAt method now returns position information alongside query value, using new helper methods isCompleteQuery and _findQueryBoundaries for robust boundary detection. Tests validate behavior with multi-line formatting.

Changes

Cohort / File(s) Summary
Query Detection Refactoring
web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js
Reworked query boundary detection logic with new _findQueryBoundaries helper for CodeMirror tree traversal and new isCompleteQuery helper to validate query completeness. Modified getQueryAt to return {value, from, to} instead of just value, with control flow now driven by completeness checks to determine boundary expansion.
Query Detection Tests
web/regression/javascript/components/CodeMirrorCustomEditor.spec.js
Added test cases validating query extraction with blank lines between SQL clauses and multi-line formatting, ensuring cursor positioning on WHERE, FROM, and condition lines correctly returns full query boundaries.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title addresses a specific issue with EXPLAIN and EXPLAIN ANALYZE commands when blank lines are present in SQL queries, which aligns with the main changes in the PR that restructure query boundary detection and completeness checking to handle blank lines properly.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js`:
- Around line 238-240: Clamp startPos to the document length as well as the
lower bound: after computing startPos (which can be set to currLine.to + 1 in
the codepath around where currLine is used), add an upper-bound check similar to
endPos so if startPos > this.state.doc.length set startPos =
this.state.doc.length; ensure you update the existing bounds block that
currently checks "if(startPos < 0) startPos = 0;" and "if(endPos >
this.state.doc.length) endPos = this.state.doc.length;" to include the startPos
upper clamp.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 97c90f1 and 26b7ab7.

📒 Files selected for processing (2)
  • web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js
  • web/regression/javascript/components/CodeMirrorCustomEditor.spec.js

Comment on lines +238 to +240
// Make sure start and end are valid values
if(startPos < 0) startPos = 0;
if(endPos > this.state.doc.length) endPos = this.state.doc.length;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Clamp startPos to the document length.

Line 239 only guards the lower bound. startPos can become currLine.to + 1 (Line 151) on a trailing blank line, which can exceed doc.length and return an invalid from. Add an upper-bound clamp alongside the existing checks.

🛠️ Proposed fix
-    if(startPos < 0) startPos = 0;
+    if(startPos < 0) startPos = 0;
+    if(startPos > this.state.doc.length) startPos = this.state.doc.length;
     if(endPos > this.state.doc.length) endPos = this.state.doc.length;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Make sure start and end are valid values
if(startPos < 0) startPos = 0;
if(endPos > this.state.doc.length) endPos = this.state.doc.length;
// Make sure start and end are valid values
if(startPos < 0) startPos = 0;
if(startPos > this.state.doc.length) startPos = this.state.doc.length;
if(endPos > this.state.doc.length) endPos = this.state.doc.length;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@web/pgadmin/static/js/components/ReactCodeMirror/CustomEditorView.js` around
lines 238 - 240, Clamp startPos to the document length as well as the lower
bound: after computing startPos (which can be set to currLine.to + 1 in the
codepath around where currLine is used), add an upper-bound check similar to
endPos so if startPos > this.state.doc.length set startPos =
this.state.doc.length; ensure you update the existing bounds block that
currently checks "if(startPos < 0) startPos = 0;" and "if(endPos >
this.state.doc.length) endPos = this.state.doc.length;" to include the startPos
upper clamp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant